<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.contrib.auth.views &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../index.html" />
    <link rel="up" title="django.contrib.auth" href="../auth.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-auth-views">
            
  <h1>Source code for django.contrib.auth.views</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponseRedirect</span><span class="p">,</span> <span class="n">QueryDict</span>
<span class="kn">from</span> <span class="nn">django.template.response</span> <span class="kn">import</span> <span class="n">TemplateResponse</span>
<span class="kn">from</span> <span class="nn">django.utils.http</span> <span class="kn">import</span> <span class="n">is_safe_url</span><span class="p">,</span> <span class="n">urlsafe_base64_decode</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves.urllib.parse</span> <span class="kn">import</span> <span class="n">urlparse</span><span class="p">,</span> <span class="n">urlunparse</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">resolve_url</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.debug</span> <span class="kn">import</span> <span class="n">sensitive_post_parameters</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.cache</span> <span class="kn">import</span> <span class="n">never_cache</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.csrf</span> <span class="kn">import</span> <span class="n">csrf_protect</span>

<span class="c"># Avoid shadowing the login() and logout() views below.</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="kn">import</span> <span class="p">(</span><span class="n">REDIRECT_FIELD_NAME</span><span class="p">,</span> <span class="n">login</span> <span class="k">as</span> <span class="n">auth_login</span><span class="p">,</span>
    <span class="n">logout</span> <span class="k">as</span> <span class="n">auth_logout</span><span class="p">,</span> <span class="n">get_user_model</span><span class="p">,</span> <span class="n">update_session_auth_hash</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.decorators</span> <span class="kn">import</span> <span class="n">login_required</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.forms</span> <span class="kn">import</span> <span class="n">AuthenticationForm</span><span class="p">,</span> <span class="n">PasswordResetForm</span><span class="p">,</span> <span class="n">SetPasswordForm</span><span class="p">,</span> <span class="n">PasswordChangeForm</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.tokens</span> <span class="kn">import</span> <span class="n">default_token_generator</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.shortcuts</span> <span class="kn">import</span> <span class="n">get_current_site</span>


<span class="nd">@sensitive_post_parameters</span><span class="p">()</span>
<span class="nd">@csrf_protect</span>
<span class="nd">@never_cache</span>
<div class="viewcode-block" id="login"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.login">[docs]</a><span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/login.html&#39;</span><span class="p">,</span>
          <span class="n">redirect_field_name</span><span class="o">=</span><span class="n">REDIRECT_FIELD_NAME</span><span class="p">,</span>
          <span class="n">authentication_form</span><span class="o">=</span><span class="n">AuthenticationForm</span><span class="p">,</span>
          <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Displays the login form and handles the login action.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">redirect_to</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">redirect_field_name</span><span class="p">,</span>
                                   <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">redirect_field_name</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&quot;POST&quot;</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">authentication_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>

            <span class="c"># Ensure the user-originating redirection url is safe.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">is_safe_url</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">redirect_to</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">get_host</span><span class="p">()):</span>
                <span class="n">redirect_to</span> <span class="o">=</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">LOGIN_REDIRECT_URL</span><span class="p">)</span>

            <span class="c"># Okay, security check complete. Log the user in.</span>
            <span class="n">auth_login</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">get_user</span><span class="p">())</span>

            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">redirect_to</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">authentication_form</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="n">current_site</span> <span class="o">=</span> <span class="n">get_current_site</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
        <span class="n">redirect_field_name</span><span class="p">:</span> <span class="n">redirect_to</span><span class="p">,</span>
        <span class="s">&#39;site&#39;</span><span class="p">:</span> <span class="n">current_site</span><span class="p">,</span>
        <span class="s">&#39;site_name&#39;</span><span class="p">:</span> <span class="n">current_site</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="logout"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.logout">[docs]</a><span class="k">def</span> <span class="nf">logout</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">next_page</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
           <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/logged_out.html&#39;</span><span class="p">,</span>
           <span class="n">redirect_field_name</span><span class="o">=</span><span class="n">REDIRECT_FIELD_NAME</span><span class="p">,</span>
           <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Logs out the user and displays &#39;You are logged out&#39; message.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">auth_logout</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">next_page</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">next_page</span> <span class="o">=</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">next_page</span><span class="p">)</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">redirect_field_name</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">or</span>
            <span class="n">redirect_field_name</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">):</span>
        <span class="n">next_page</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">redirect_field_name</span><span class="p">,</span>
                                     <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">redirect_field_name</span><span class="p">))</span>
        <span class="c"># Security check -- don&#39;t allow redirection to a different host.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">is_safe_url</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">next_page</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">get_host</span><span class="p">()):</span>
            <span class="n">next_page</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span>

    <span class="k">if</span> <span class="n">next_page</span><span class="p">:</span>
        <span class="c"># Redirect to this page until the session has been cleared.</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">next_page</span><span class="p">)</span>

    <span class="n">current_site</span> <span class="o">=</span> <span class="n">get_current_site</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;site&#39;</span><span class="p">:</span> <span class="n">current_site</span><span class="p">,</span>
        <span class="s">&#39;site_name&#39;</span><span class="p">:</span> <span class="n">current_site</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Logged out&#39;</span><span class="p">)</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
        <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="logout_then_login"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.logout_then_login">[docs]</a><span class="k">def</span> <span class="nf">logout_then_login</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">login_url</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Logs out the user if they are logged in. Then redirects to the log-in page.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">login_url</span><span class="p">:</span>
        <span class="n">login_url</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">LOGIN_URL</span>
    <span class="n">login_url</span> <span class="o">=</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">login_url</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">logout</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">login_url</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="n">extra_context</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="redirect_to_login"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.redirect_to_login">[docs]</a><span class="k">def</span> <span class="nf">redirect_to_login</span><span class="p">(</span><span class="nb">next</span><span class="p">,</span> <span class="n">login_url</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                      <span class="n">redirect_field_name</span><span class="o">=</span><span class="n">REDIRECT_FIELD_NAME</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Redirects the user to the login page, passing the given &#39;next&#39; page</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">resolved_url</span> <span class="o">=</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">login_url</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">LOGIN_URL</span><span class="p">)</span>

    <span class="n">login_url_parts</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">urlparse</span><span class="p">(</span><span class="n">resolved_url</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">redirect_field_name</span><span class="p">:</span>
        <span class="n">querystring</span> <span class="o">=</span> <span class="n">QueryDict</span><span class="p">(</span><span class="n">login_url_parts</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">mutable</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="n">querystring</span><span class="p">[</span><span class="n">redirect_field_name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">next</span>
        <span class="n">login_url_parts</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">querystring</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">safe</span><span class="o">=</span><span class="s">&#39;/&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">urlunparse</span><span class="p">(</span><span class="n">login_url_parts</span><span class="p">))</span>


<span class="c"># 4 views for password reset:</span>
<span class="c"># - password_reset sends the mail</span>
<span class="c"># - password_reset_done shows a success message for the above</span>
<span class="c"># - password_reset_confirm checks the link the user clicked and</span>
<span class="c">#   prompts for a new password</span>
<span class="c"># - password_reset_complete shows a success message for the above</span>
</div>
<span class="nd">@csrf_protect</span>
<div class="viewcode-block" id="password_reset"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.password_reset">[docs]</a><span class="k">def</span> <span class="nf">password_reset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">is_admin_site</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                   <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/password_reset_form.html&#39;</span><span class="p">,</span>
                   <span class="n">email_template_name</span><span class="o">=</span><span class="s">&#39;registration/password_reset_email.html&#39;</span><span class="p">,</span>
                   <span class="n">subject_template_name</span><span class="o">=</span><span class="s">&#39;registration/password_reset_subject.txt&#39;</span><span class="p">,</span>
                   <span class="n">password_reset_form</span><span class="o">=</span><span class="n">PasswordResetForm</span><span class="p">,</span>
                   <span class="n">token_generator</span><span class="o">=</span><span class="n">default_token_generator</span><span class="p">,</span>
                   <span class="n">post_reset_redirect</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                   <span class="n">from_email</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                   <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                   <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                   <span class="n">html_email_template_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">post_reset_redirect</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">post_reset_redirect</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s">&#39;password_reset_done&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">post_reset_redirect</span> <span class="o">=</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">post_reset_redirect</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&quot;POST&quot;</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">password_reset_form</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s">&#39;use_https&#39;</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">is_secure</span><span class="p">(),</span>
                <span class="s">&#39;token_generator&#39;</span><span class="p">:</span> <span class="n">token_generator</span><span class="p">,</span>
                <span class="s">&#39;from_email&#39;</span><span class="p">:</span> <span class="n">from_email</span><span class="p">,</span>
                <span class="s">&#39;email_template_name&#39;</span><span class="p">:</span> <span class="n">email_template_name</span><span class="p">,</span>
                <span class="s">&#39;subject_template_name&#39;</span><span class="p">:</span> <span class="n">subject_template_name</span><span class="p">,</span>
                <span class="s">&#39;request&#39;</span><span class="p">:</span> <span class="n">request</span><span class="p">,</span>
                <span class="s">&#39;html_email_template_name&#39;</span><span class="p">:</span> <span class="n">html_email_template_name</span><span class="p">,</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="n">is_admin_site</span><span class="p">:</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">opts</span><span class="p">,</span> <span class="n">domain_override</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">get_host</span><span class="p">())</span>
            <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="o">**</span><span class="n">opts</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">post_reset_redirect</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">password_reset_form</span><span class="p">()</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
        <span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Password reset&#39;</span><span class="p">),</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="password_reset_done"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.password_reset_done">[docs]</a><span class="k">def</span> <span class="nf">password_reset_done</span><span class="p">(</span><span class="n">request</span><span class="p">,</span>
                        <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/password_reset_done.html&#39;</span><span class="p">,</span>
                        <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Password reset successful&#39;</span><span class="p">),</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span>


<span class="c"># Doesn&#39;t need csrf_protect since no-one can guess the URL</span></div>
<span class="nd">@sensitive_post_parameters</span><span class="p">()</span>
<span class="nd">@never_cache</span>
<div class="viewcode-block" id="password_reset_confirm"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.password_reset_confirm">[docs]</a><span class="k">def</span> <span class="nf">password_reset_confirm</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">uidb64</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                           <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/password_reset_confirm.html&#39;</span><span class="p">,</span>
                           <span class="n">token_generator</span><span class="o">=</span><span class="n">default_token_generator</span><span class="p">,</span>
                           <span class="n">set_password_form</span><span class="o">=</span><span class="n">SetPasswordForm</span><span class="p">,</span>
                           <span class="n">post_reset_redirect</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                           <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    View that checks the hash in a password reset link and presents a</span>
<span class="sd">    form for entering a new password.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">UserModel</span> <span class="o">=</span> <span class="n">get_user_model</span><span class="p">()</span>
    <span class="k">assert</span> <span class="n">uidb64</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">token</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span>  <span class="c"># checked by URLconf</span>
    <span class="k">if</span> <span class="n">post_reset_redirect</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">post_reset_redirect</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s">&#39;password_reset_complete&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">post_reset_redirect</span> <span class="o">=</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">post_reset_redirect</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">uid</span> <span class="o">=</span> <span class="n">urlsafe_base64_decode</span><span class="p">(</span><span class="n">uidb64</span><span class="p">)</span>
        <span class="n">user</span> <span class="o">=</span> <span class="n">UserModel</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">uid</span><span class="p">)</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">TypeError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">,</span> <span class="ne">OverflowError</span><span class="p">,</span> <span class="n">UserModel</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">):</span>
        <span class="n">user</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">if</span> <span class="n">user</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">token_generator</span><span class="o">.</span><span class="n">check_token</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="n">token</span><span class="p">):</span>
        <span class="n">validlink</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter new password&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;POST&#39;</span><span class="p">:</span>
            <span class="n">form</span> <span class="o">=</span> <span class="n">set_password_form</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
                <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
                <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">post_reset_redirect</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">form</span> <span class="o">=</span> <span class="n">set_password_form</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">validlink</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Password reset unsuccessful&#39;</span><span class="p">)</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
        <span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span>
        <span class="s">&#39;validlink&#39;</span><span class="p">:</span> <span class="n">validlink</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="password_reset_complete"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.password_reset_complete">[docs]</a><span class="k">def</span> <span class="nf">password_reset_complete</span><span class="p">(</span><span class="n">request</span><span class="p">,</span>
                            <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/password_reset_complete.html&#39;</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;login_url&#39;</span><span class="p">:</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">LOGIN_URL</span><span class="p">),</span>
        <span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Password reset complete&#39;</span><span class="p">),</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span>

</div>
<span class="nd">@sensitive_post_parameters</span><span class="p">()</span>
<span class="nd">@csrf_protect</span>
<span class="nd">@login_required</span>
<div class="viewcode-block" id="password_change"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.password_change">[docs]</a><span class="k">def</span> <span class="nf">password_change</span><span class="p">(</span><span class="n">request</span><span class="p">,</span>
                    <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/password_change_form.html&#39;</span><span class="p">,</span>
                    <span class="n">post_change_redirect</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                    <span class="n">password_change_form</span><span class="o">=</span><span class="n">PasswordChangeForm</span><span class="p">,</span>
                    <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">post_change_redirect</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">post_change_redirect</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s">&#39;password_change_done&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">post_change_redirect</span> <span class="o">=</span> <span class="n">resolve_url</span><span class="p">(</span><span class="n">post_change_redirect</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&quot;POST&quot;</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">password_change_form</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
            <span class="c"># Updating the password logs out all other sessions for the user</span>
            <span class="c"># except the current one if</span>
            <span class="c"># django.contrib.auth.middleware.SessionAuthenticationMiddleware</span>
            <span class="c"># is enabled.</span>
            <span class="n">update_session_auth_hash</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">post_change_redirect</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">password_change_form</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
        <span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Password change&#39;</span><span class="p">),</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span>

</div>
<span class="nd">@login_required</span>
<div class="viewcode-block" id="password_change_done"><a class="viewcode-back" href="../../../../topics/auth/default.html#django.contrib.auth.views.password_change_done">[docs]</a><span class="k">def</span> <span class="nf">password_change_done</span><span class="p">(</span><span class="n">request</span><span class="p">,</span>
                         <span class="n">template_name</span><span class="o">=</span><span class="s">&#39;registration/password_change_done.html&#39;</span><span class="p">,</span>
                         <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Password change successful&#39;</span><span class="p">),</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span>
                            <span class="n">current_app</span><span class="o">=</span><span class="n">current_app</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../index.html">Module code</a>
        
          <ul><li><a href="../../../django.html">django</a>
        
          <ul><li><a href="../auth.html">django.contrib.auth</a>
        
        <ul><li>django.contrib.auth.views</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>